﻿using CefSharp;
using CefSharp.Wpf;
using System;
using System.Threading;
using System.Windows;
using System.Windows.Forms;

namespace YoudaoNote
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 浏览器
        /// </summary>
        private ChromiumWebBrowser browser;
        /// <summary>
        /// 通知图标
        /// </summary>
        private NotifyIcon notifyIcon = null;

        public MainWindow()
        {
            InitializeComponent();
            InitializeChromium();
            InitializeNotifyIcon();
        }

        #region 通知图标
        /// <summary>
        /// 初始化通知图标
        /// </summary>
        private void InitializeNotifyIcon()
        {
            //设置托盘的各个属性
            notifyIcon = new NotifyIcon();
            notifyIcon.BalloonTipText = "有道云笔记";
            notifyIcon.ShowBalloonTip(500);
            notifyIcon.Text = notifyIcon.BalloonTipText;
            notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
            notifyIcon.Visible = true;

            // 打开菜单项
            MenuItem open = new MenuItem("显示窗口");
            open.Click += new EventHandler(OpenWindow);
            // 退出菜单项
            MenuItem exit = new MenuItem("退出程序");
            exit.Click += new EventHandler(AppExit);
            // 关联托盘控件
            MenuItem[] childen = new MenuItem[] { open, exit };
            notifyIcon.ContextMenu = new ContextMenu(childen);
            this.notifyIcon.MouseClick += new MouseEventHandler((o, e) =>
            {
                if (e.Button == MouseButtons.Left) this.OpenWindow(o, e);
            });
        }

        /// <summary>
        /// 显示窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenWindow(object sender, EventArgs e)
        {
            this.Visibility = Visibility.Visible;
            this.WindowState = WindowState.Normal;
            this.ShowInTaskbar = true;
            this.Activate();
        }

        /// <summary>
        /// 程序退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AppExit(object sender, EventArgs e)
        {
            System.Windows.Application.Current.Shutdown();
        }
        #endregion

        /// <summary>
        /// 初始化浏览器
        /// </summary>
        private void InitializeChromium()
        {
            // 参数 设置
            CefSettings settings = new CefSettings();
            settings.CachePath = AppDomain.CurrentDomain.BaseDirectory + "/cache";
            settings.CefCommandLineArgs.Add("disable-application-cache", "1");
            settings.CefCommandLineArgs.Add("disable-session-storage", "1");
            settings.MultiThreadedMessageLoop = true;
            if (Cef.IsInitialized == false) Cef.Initialize(settings);
            // 创建实例
            browser = new ChromiumWebBrowser("https://note.youdao.com/web/");
            browser.FrameLoadEnd += Browser_FrameLoadEnd;
            // 添加控件
            this.MainContainer.Children.Add(browser);
        }

        /// <summary>
        /// 执行JS
        /// </summary>
        /// <param name="js"></param>
        private void ExecuteJavaScriptAsync(string js)
        {
            this.Dispatcher.BeginInvoke(new Action(()=> {
                browser.GetBrowser().MainFrame.ExecuteJavaScriptAsync(js);
            }));
        }

        /// <summary>
        /// 浏览器页面加载完毕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
        {

            ThreadPool.QueueUserWorkItem(_ =>
            {
                int i = 100;
                while (i >= 0)
                {
                    // 登录页
                    ExecuteJavaScriptAsync("$('.hd').find('div.nav').remove();");
                    ExecuteJavaScriptAsync("$('.login-main').find('div.login-right').remove();");
                    ExecuteJavaScriptAsync("$('.login-main').find('div.login-middle').remove();");
                    ExecuteJavaScriptAsync("$('.login-main').width(400);");
                    ExecuteJavaScriptAsync("$('div.ft').remove();");

                    // 功能页面
                    ExecuteJavaScriptAsync("$('.group-tab').remove();");
                    ExecuteJavaScriptAsync("$('.top-right').find('a.to-website').remove();");
                    ExecuteJavaScriptAsync("$('.top-right').find('i.top-line').remove();");
                    ExecuteJavaScriptAsync("$('.top-right').find('a.update-vip').remove();");
                    ExecuteJavaScriptAsync("$('.top-right').find('a.pc-btn').remove();");

                    i--;
                    Thread.Sleep(TimeSpan.FromMilliseconds(100));
                }
            });
        }

        /// <summary>
        /// 窗口关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {

            this.Visibility = Visibility.Hidden;
            this.WindowState = WindowState.Normal;
            this.ShowInTaskbar = true;
            e.Cancel = true;
        }
    }
}
